package data.courseselectiondata;

import java.io.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;

import dataservice.courseselectiondataservice.StateDataService;

public class StateData extends UnicastRemoteObject implements StateDataService {

	public StateData() throws RemoteException {

	}

	@Override
	public boolean start() throws RemoteException {
		try {
			File file = new File("State.txt");
			Scanner s = new Scanner(file);
			int result = s.nextInt();
			s.close();
			if (result == 0) {
				FileWriter f = new FileWriter(file);
				f.write("1");
				f.close();
				return true;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	public boolean quit() throws RemoteException {
		try {
			File file = new File("State.txt");
			Scanner s = new Scanner(file);
			int result = s.nextInt();
			s.close();
			if (result == 1) {
				FileWriter f = new FileWriter(file);
				f.write("2");
				f.close();
				return true;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	@Override
	public boolean end() throws RemoteException {
		try {
			File file = new File("State.txt");
			Scanner s = new Scanner(file);
			int result = s.nextInt();
			s.close();
			if (result == 1 || result == 2) {
				FileWriter fw = new FileWriter(file);
				fw.write("0");
				fw.close();
				return true;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	public boolean isStarted() throws RemoteException {
		try {
			File file = new File("State.txt");
			Scanner s = new Scanner(file);
			int result = s.nextInt();
			s.close();
			if (result == 1) {
				return true;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean isQuit() throws RemoteException {
		try {
			File file = new File("State.txt");
			Scanner s = new Scanner(file);
			int result = s.nextInt();
			s.close();
			if (result == 2) {
				return true;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
